package com.xh.bussiness.tiku.util;

import com.alibaba.fastjson.JSON;
import com.xh.bussiness.tiku.bean.dto.AnswerResultDto;
import com.xh.bussiness.tiku.bean.dto.QuestionOptionDto;
import com.xh.bussiness.tiku.constant.QuestionConstant;
import com.xh.core.util.MathUtils;
import com.xh.core.util.StringUtils;
import com.xh.tools.question.constant.QuestionTypeEnum;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class CheckQuestionUtil {


    /**
     * @param subjectId
     * @param type        试题基础类型
     * @param rightAnswer 试题正确答案
     * @param userAnswer
     * @return
     */
    public static AnswerResultDto checkAnswer(String subjectName, Integer type, String rightAnswer, String userAnswer,Double score) {
        //题型为空，无法自动判题
        if (type == null) {
            return new AnswerResultDto(false,  QuestionConstant.ANSWER_RESULT.TEACHER_CORRECT, 0, 0,null);
        }
        //试题没有答案，无法自动判题
        if (StringUtils.isEmpty(rightAnswer)) {
            return new AnswerResultDto(false,  QuestionConstant.ANSWER_RESULT.TEACHER_CORRECT, 0, 0,null);
        }
        List<QuestionOptionDto> rightAnswerList = JSON.parseArray(rightAnswer, QuestionOptionDto.class);
        //支持单选题、多选题、判断题、英语阅读理解、英语完形填空自动判题
        if (type.equals(QuestionTypeEnum.SINGLE.getValue()) || type.equals(QuestionTypeEnum.MUL.getValue()) || type.equals(QuestionTypeEnum.JUDGE.getValue()) ||
                type.equals(QuestionTypeEnum.READING.getValue()) || type.equals(QuestionTypeEnum.CLOZE.getValue())) {
            //未作答
            if (StringUtils.isEmpty(userAnswer)) {
                return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.UN_ANSWER, 0, 0,0.0);
            }
            List<QuestionOptionDto> userAnswerList = JSON.parseArray(userAnswer, QuestionOptionDto.class);
            userAnswerList = userAnswerList.stream().sorted(Comparator.comparing(QuestionOptionDto::getNo)).collect(Collectors.toList());
            rightAnswerList = rightAnswerList.stream().sorted(Comparator.comparing(QuestionOptionDto::getNo)).collect(Collectors.toList());
            //英语阅读理解、英语完形填空
            if ((type.equals(QuestionTypeEnum.READING.getValue()) || type.equals(QuestionTypeEnum.CLOZE.getValue())) && (subjectName.contains("英语"))) {
                if (userAnswerList.size() != rightAnswerList.size()) {
                    return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.WRONG, 0, 0,0.0);
                }
                //做对个数
                Integer rightCount = rightAnswerList.size();
                for (int i = 0; i < rightAnswerList.size(); i++) {
                    QuestionOptionDto userAnswerDto = userAnswerList.get(i);
                    if (!rightAnswerList.get(i).getContent().equals(userAnswerDto.getContent())) {
                        rightCount -- ;
                    }
                }
                //判断是否全部答对
                if (rightCount.equals(rightAnswerList.size())) {
                    return new AnswerResultDto(true, QuestionConstant.ANSWER_RESULT.RIGHT, rightAnswerList.size(), rightCount,score);
                }else{
                    //获取每一道小题分数
                    double subQuestionScore = MathUtils.divTwoNumberRoundHalfUp(score, Integer.valueOf(rightAnswerList.size()), MathUtils.DEFAULT_SCALE);
                    //大题得分
                    double questionScore = subQuestionScore * rightCount;
                    return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.WRONG, rightAnswerList.size(), rightCount, questionScore);
                }
            }
            //单选题、多选题
            else if (type.equals(QuestionTypeEnum.SINGLE.getValue()) || type.equals(QuestionTypeEnum.MUL.getValue())) {
                if (rightAnswerList.size() != userAnswerList.size()) {
                    return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.WRONG, 0, 0,0.0);
                }
                for (int i = 0; i < rightAnswerList.size(); i++) {
                    //选项内容不同直接作答错误
                    if (!rightAnswerList.get(i).getName().equals(userAnswerList.get(i).getName())) {
                        return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.WRONG, 0, 0,0.0);
                    }
                }
                //作答正确
                return new AnswerResultDto(true, QuestionConstant.ANSWER_RESULT.RIGHT, 0, 0,score);
            }
            //判断题
            else if (type.equals(QuestionTypeEnum.JUDGE.getValue())) {
                QuestionOptionDto questionOptionDTO = rightAnswerList.get(0);
                QuestionOptionDto userAnswerDTO = userAnswerList.get(0);
                if (!questionOptionDTO.getContent().equals(userAnswerDTO.getContent())) {
                    //作答错误
                    return new AnswerResultDto(false, QuestionConstant.ANSWER_RESULT.WRONG, 0, 0, 0.0);
                }
                //作答正确
                return new AnswerResultDto(true, QuestionConstant.ANSWER_RESULT.RIGHT, 0, 0,score);
            }
        }
        return new AnswerResultDto(false,  QuestionConstant.ANSWER_RESULT.TEACHER_CORRECT, 0, 0,null);
    }

}
